require(['init'], function () {
    // var $pagination = $('#js-pagination')
    // var pagination
    getstudent_order()
    function getstudent_order() {
        $.ajax({
            url: '/api/user/current'
        })
            .done(function (res) {
                $.ajax({
                    url: '/api/student/user/' + res.data.userid
                })
                    .done(function (res) {
                        $.ajax({
                            url: "/api/orders/student/" + res.data.studentuid,
                            method: "get"
                        })
                            .done(function (res) {
                                $.ajax({
                                    url: '/api/payment/1',
                                    type: 'get',
                                    success: function (json) {
                                        reader_order(res, json)
                                    }
                                })
                            })
                    })
            })
    }

    //渲染页面
    var i = "";

    function reader_order(data, json) {
        console.log(data)
        console.log(json)
        var $student_order = $('#student_orderlist').html("");
        $.each(data.data, function (index, item) {
            var $tr = $('<tr class="enroll" style="position:relative"></tr>').appendTo($student_order)
            $tr.append('<td>' + (index + 1) + '</td>');
            $tr.append('<td>' + item.id + '</td>');
            $tr.append('<td>' + item.productname + '</td>');
            $tr.append('<td>' + item.created_at + '</td>');
            var $status = $('<td></td>').appendTo($tr);
            $tr.append('<td>' + item.amount + '</td>');
            var $operation = $('<td></td>').appendTo($tr);
            if (item.status == 0) {
                var $cancelled = $('<a style="color:red">' + i18next.t('table.canceled') + '</a>').appendTo($status);
                var $cancelled = $('<a >' + i18next.t('table.canceled') + '</a>').appendTo($operation);
            } else if (item.status == 1) {
                var $for_payment = $('<a style="color:red">' + i18next.t('table.unpaid') + '</a>').appendTo($status);
                var $goby = $('<button class="	btn btn-primary btn-sm btn-inverse" style="margin-right:6px;">' + i18next.t('table.confirm') + '</button>').appendTo($operation);
                $for_payment.parent().parent('tr').addClass("len");
                $goby.on('click', function () {
                    var $dom = $('<div class="a_div" style="height:40px;line-height:40px">' + i18next.t('table.ordercancel') + '</div>\
         <ul class="s_ul">\
           <li><span>' + i18next.t('table.ordernumber') + '</span>　　　<a>' + item.id + '</a></li>\
           <li><span>' + i18next.t('table.ordername') + '</span>　　<a>' + item.productname + '</a></li>\
           <li><span>' + i18next.t('table.buyer') + '</span>　　　<a>' + item.studentname + '</a></li>\
           <li><span>' + i18next.t('table.orderprice') + '</span>　　<a style="color:red">' + item.amount + '</a><i style="font-style:normal; margin-left:6px;">' + i18next.t('table.yuan') + '</i></li>\
           <li><span>' + i18next.t('table.createtime') + '</span>　　<a>' + item.created_at + '</a></li>\
           <li><span>' + i18next.t('table.contactnumber') + '</span>　　<a>' + item.mobile + '</a></li>\
           <li style="height:100%;line-height: 30px;">\
            <span>' + i18next.t('table.paymentmethod') + '</span><span style="margin-left: 28px;color: #2a87f3">1.' + i18next.t('table.transferpayment') + '</span>　\
            <div style="padding-left: 15px"><div class="formTitle">\
            <span class="title">' + i18next.t('table.unitname') + ':</span>' + json.data.company + '</div>\
            <div class="formTitle"><span class="title">' + i18next.t('table.bankaccount') + ':</span>' + json.data.account + '</div> \
            <div class="formTitle"><span class="title">' + i18next.t('table.bankaccountname') + ':</span>' + json.data.name + '</div>\
             <div class="formTitle">' + i18next.t('table.scancodepayment') + '：</div>\
               <div class="formTitle">\
                  <div style="display:inline-block">\
                    <p>' + i18next.t('table.webchatqrcode') + '</p>\
                    <img style="width: 120px;height: 120px;" src="' + json.data.webchatqrcode + '" alt="">\
                  </div>\
                  <div style="display:inline-block">\
                    <p>' + i18next.t('table.alipayqrcode') + '</p>\
                    <img style="width: 120px;height: 120px;" src="' + json.data.alipayqrcode + '" alt="">\
                  </div>\
               </div>\
             </div>\
           </li>\
         </ul>');
                    var popUpBox = new PopUpBox({
                        type: 'dom',
                        style: 'default contentScroll',
                        submitBtnType: 'alert',
                        titleText: i18next.t('popupbox.orderconfirmation'),//订单确认
                        show: true,
                        content: $dom,
                        callback: {
                            yes: function (e) {
                                $.ajax({
                                    url: "/api/order/" + item.id + "/confirm",
                                    method: "put",
                                    success: function (data) {
                                        window.location.reload();
                                    }
                                })
                            }
                        },
                        position: {
                            width: 671,
                            height: 510
                        }
                    })
                    $('.title').css("margin-right", "15px")
                    $('.formTitle').css({"color": "#2a87f3", "margin-left": "70px"});
                    $('.submitBtnBox .btn').html(i18next.t('table.confirmpayment'));//确认支付
                })

                var $cancel_order = $('<button  class="btn btn-danger btn-sm btn-inverse">' + i18next.t('table.cancel') + '</button>').appendTo($operation);
                $cancel_order.on('click', function () {
                    var popUpBox = new PopUpBox({
                        type: 'dom',
                        style: 'default contentScroll',
                        submitBtnType: 'confirm',
                        titleText: i18next.t('table.orderdetails'),//订单详情
                        show: true,
                        content: "<div style='margin-left:27px;margin-top:34px;'>确定要取消订单么?</div>",
                        callback: {
                            yes: function (e) {
                                $.ajax({
                                    url: "/api/order/" + item.id + "/cancel",
                                    method: "put",
                                    success: function (data) {
                                        window.location.reload();
                                    }
                                })
                            },
                        },
                        position: {
                            width: 448,
                            height: 226
                        }
                    })

                })
            } else if (item.status == 2) {
                var $payment = $('<a>' + i18next.t('table.inpayment') + '</a>').appendTo($status);
                var $failureby = $('<a class="text-primary">' + i18next.t('table.orderdetails') + '</a>').appendTo($operation);
                $failureby.on('click', function () {
                    var $dom = $('<div class="a_div" style="height:40px;line-height:40px">' + i18next.t('table.orderok') + '</div>\
         <ul class="s_ul">\
           <li><span>' + i18next.t('table.ordernumber') + '</span>　　　<a>' + item.id + '</a></li>\
           <li><span>' + i18next.t('table.ordername') + '</span>　　<a>' + item.productname + '</a></li>\
           <li><span>' + i18next.t('table.buyer') + '</span>　　　<a>' + item.studentname + '</a></li>\
           <li><span>' + i18next.t('table.orderprice') + '</span>　　<a style="color:red">' + item.amount + '</a><i style="font-style:normal; margin-left:6px;" >' + i18next.t('table.yuan') + '</i></li>\
           <li><span>' + i18next.t('table.createtime') + '</span>　　<a>' + item.created_at + '</a></li>\
           <li><span>' + i18next.t('table.contactnumber') + '</span>　　<a>' + item.mobile + '</a></li>\
           <li style="height:120px;"><span>' + i18next.t('table.paymentmethod') + '</span>　　<a style="color:red;">1.' + i18next.t('table.pay') + '<br>　　　　　　2.' + i18next.t('table.call') + '<br>　　　　　　3.' + i18next.t('table.webchatscancode') + '</a></li>\
         </ul>');
                    var popUpBox = new PopUpBox({
                        type: 'dom',
                        style: 'default contentScroll',
                        submitBtnType: false,
                        titleText: i18next.t('table.orderdetails'),//订单详情
                        show: true,
                        content: $dom,
                        callback: {
                            yes: function (e) {
                                window.location.reload();
                            },
                        },
                        position: {
                            width: 780,
                            height: 508
                        }
                    })
                })
            } else if (item.status == 3) {
                var $failureby = $('<a style="color:#32ab9e">' + i18next.t('table.alreadypaid') + '</a>').appendTo($status);
                var $failureby = $('<a>' + i18next.t('table.alreadypaid') + '</a>').appendTo($operation);
                $failureby.parent().parent('tr').addClass("byed");
            } else if (item.status == 4) {
                var $confirm = $('<a  style="yellow">' + i18next.t('table.branchclass') + '</a>').appendTo($status);
            } else if (item.status == 5) {
                var $expired1 = $('<a>' + i18next.t('table.overdue') + '</a>').appendTo($status);
            }
        })
        //带支付的个数
        l = $('.len').length;
        $("#payment_etclen").html(l);
        $(".all_order").on('click', function () {
            $(this).addClass("s_hover")
            $('.payment_etc').removeClass('s_hover');
            $('.payment_has').removeClass('s_hover');
            $student_order.find('.enroll').show();
        })
        $(".payment_etc").on('click', function () {
            $(this).addClass("s_hover")
            $('.all_order').removeClass('s_hover');
            $('.payment_has').removeClass('s_hover');
            $student_order.find('.enroll')
                .show()
            $student_order.find('tr:not(.len)')
                .hide();
        })
        $(".payment_has").on('click', function () {
            $(this).addClass("s_hover")
            $('.all_order').removeClass('s_hover');
            $('.payment_etc').removeClass('s_hover');
            $student_order.find('.enroll')
                .show()
            $student_order.find('tr:not(.byed)')
                .hide();
        })
    }

    //pagination = new Pagination($pagination,getstudent_order);
})
